<template>
  <div class="header-container">
    <div class="center">
      <label for="input-todo"><span>ToDoList</span></label>
      <input id="input-todo" ref="inputRef" type="text" placeholder="添加您的待办事项（按enter键提交）" @keyup.enter="addTodoItem" @keyup.esc="clearInput" />
    </div>
  </div>
</template>

<script>
import getUniqueId from '@/util/getUniqueId.js'
export default {
  name: 'Header',
  methods: {
    addTodoItem() {
      console.log(this.$store)
      if (this.$refs.inputRef.value === '') return alert('请输入内容')
      const todoObj = {
        id: getUniqueId(),
        comName: 'ListItem',
        content: this.$refs.inputRef.value,
        finished: false
      }
      this.$store.commit('ADD_TODO', todoObj)
      this.clearInput()
    },
    // 清空输入框的数据
    clearInput() {
      this.$refs.inputRef.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.header-container {
  align-items: center;
  height: 50px;
  background-color: #323232;
  .center {
    display: flex;
    justify-content: space-between;
    align-items: center;
    label {
      cursor: pointer;
    }
    span {
      font-size: 24px;
      color: #ddd;
    }
    input {
      width: 60%;
      height: 24px;
      border-radius: 5px;
      box-shadow: 0 1px 2px rgb(255, 255, 255);
      padding: 1px 0;
      text-indent: 0.8em;
    }
  }
}
</style>
